package Ioperate;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class DelBook implements Ioperate{
    public void work(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        int count = bookList.getSize();
        int i = 0;
        int pos = -1;

        System.out.println("请输入要删除的书名：");
        String name = scanner.nextLine();

        for(i=0;i<count;i++){
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(i==count){
            System.out.println("此书不存在！");
            return ;
        }

        for (int j = pos; j < count-1; j++) {
            bookList.setBooks(j,bookList.getBooks(j+1));
        }

        bookList.setBooks(count-1,null);
        bookList.setSize(count-1);
        System.out.println("删除成功！");
        System.out.println();
    }
}
